package com.pk.platform.business.question.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.pk.platform.business.core.dao.impl.GenericDaoImpl;
import com.pk.platform.business.question.dao.IPaperDefineDao;
import com.pk.platform.business.question.vo.PaperVO;
import com.pk.platform.domain.question.Question;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.constant.Constant;
import com.pk.platform.util.constant.QuestionConstant;
import com.pk.platform.util.page.Pager;

public class PaperDefineDaoImpl extends GenericDaoImpl implements
		IPaperDefineDao {

	public List<Map<String, Object>> queryPaperByPage(PaperVO pvo, Pager pager)
	{
		StringBuffer sql = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		sql.append("select p.* from paper p where 1 = 1");
		if(StringConverter.isNotNull(pvo.getPaperName()))
		{
			sql.append(" and p.paperName like ?");
			param.add("%" + pvo.getPaperName() + "%");
		}
		if(StringConverter.isNotNull(pvo.getPaperCode()))
		{
			sql.append(" and p.paperCode like ?");
			param.add("%" + pvo.getPaperCode() + "%");
		}
		if(StringConverter.isNotNull(pvo.getPaperTimeStart()))
		{
			sql.append(" and p.paperTime >= ?");
			param.add(pvo.getPaperTimeStart());
		}
		if(StringConverter.isNotNull(pvo.getPaperTimeEnd()))
		{
			sql.append(" and p.paperTime <= ?");
			param.add(pvo.getPaperTimeEnd() + Constant.LAST_DAY_TIME);
		}
		sql.append(" order by p.paperTime desc");
		return this.getListByPage(sql.toString(), param.toArray(), pager);
	}
	
	public List<Map<String, Object>> queryCodeTableByPaper(String paperId, String ctType)
	{
		String sql = "select * from code_table where paperId = ? and ctType = ? order by code";
		return this.getSjt().queryForList(sql, new Object[]{paperId, ctType});
	}
	
	public List<Map<String, Object>> queryItemOptionByCodeTable(String ctId)
	{
		String sql = "select * from item_option where ctId = ? order by optionValue";
		return this.getSjt().queryForList(sql, ctId);
	}
	
	public List<Map<String, Object>> queryQuestionByPage(PaperVO pvo, Pager pager)
	{
		StringBuffer sql = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		sql.append("select q.*, ct.code as codetb, ct2.code as codeLabel, bd.dataValue from question q");
		sql.append(" left join code_table ct on q.ctId = ct.id");
		sql.append(" left join code_table ct2 on q.groupQes = ct2.id");
		sql.append(" left join p_basedata bd on q.qesType = bd.id");
		sql.append(" where 1 = 1 and q.paperId = ?");
		param.add(pvo.getPaperId());
		
		sql.append(" order by q.qesCode");
		if(StringConverter.isNotNull(pvo.getQesTitle()))
		{
			sql.append(" and q.qesTitle like ?");
			param.add("%" + pvo.getQesTitle() + "%");
		}
		return this.getListByPage(sql.toString(), param.toArray(), pager);
	}
	
	public boolean existQuestionCode(String paperId, String qesCode, String id)
	{
		StringBuffer sql = new StringBuffer();
		sql.append("select count(0) from question q where q.paperId = ? and q.qesCode = ?");
		List<Object> param = new ArrayList<Object>();
		param.add(paperId);
		param.add(qesCode);
		if(StringConverter.isNotNull(id))
		{
			sql.append(" and q.id <> ?");
			param.add(id);
		}
		int count = this.getSjt().queryForInt(sql.toString(), param.toArray());
		if(count > 0)
		{
			return true;
		}
		return false;
	}

	public boolean existCodeTableCode(String paperId, String code, String id)
	{
		StringBuffer sql = new StringBuffer();
		sql.append("select count(0) from code_table ct where ct.paperId = ? and ct.code = ?");
		List<Object> param = new ArrayList<Object>();
		param.add(paperId);
		param.add(code);
		if(StringConverter.isNotNull(id))
		{
			sql.append(" and ct.id <> ?");
			param.add(id);
		}
		int count = this.getSjt().queryForInt(sql.toString(), param.toArray());
		if(count > 0)
		{
			return true;
		}
		return false;
	}
	
	public Question queryQuestionByCode(String code)
	{
		String hql = "from Question q where q.qesCode = ?";
		List<Question> list = this.getHibernateTemplate().find(hql, code);
		if(list.size() > 0)
		{
			return list.get(0);
		}
		return null;
	}
	
	public void delQuestionKeyByItemOption(String qesId, String ioKey)
	{
		String sql = "delete from question_key where qesId = ? and qesKey = ?";
		this.getSjt().update(sql, new Object[]{qesId, ioKey});
	}
	
	public void delQuestionKeyByHalfOpen(String qesId)
	{
		String sql = "delete from question_key where qesId = ? and fitType = ?";
		this.getSjt().update(sql, new Object[]{qesId, QuestionConstant.FIT_TYPE_OPEN});
	}
	
}
